package com.ruoyi.web.controller.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.business.service.IUserServer;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.UserInfo;
import com.ruoyi.common.core.domain.model.AppUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.AppLoginService;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api("C端用户管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

	@Autowired
	private IUserServer userServer;
	@Autowired
	private AppLoginService loginServer;
	/**
	 * 
	 * siginup:用户注册 <br/>  
	 * 
	 * @author tomcat
	 * @return 
	 * @since JDK 1.8
	 * date: 2022年4月1日 下午3:40:53 <br/>
	 */
	@ApiOperation("用户注册")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String", dataTypeClass = String.class,required = true),
        @ApiImplicitParam(name = "password", value = "密码", dataType = "String", dataTypeClass = String.class,required = true),
        @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", dataTypeClass = String.class,required = true),
        @ApiImplicitParam(name = "qq", value = "QQ", dataType = "String", dataTypeClass = String.class,required = false)
    })
	@PostMapping("/signup")
	public AjaxResult siginup(String phone,String password,String code,String qq)
	{
		 if (StringUtils.isNull(phone) || StringUtils.isNull(password))
	        {
	            return error("参数不能为空");
	        }
		 
		UserInfo userinfo = userServer.signup(phone, password, code, qq);
		if(ObjectUtil.isNotNull(userinfo)) {
			
			AppUser appuser=	loginServer.login(userinfo, password);
			return AjaxResult.success(appuser);
		}else {
			 return error("系统繁忙");
			
		}
		
	}
	
	@ApiOperation("用户登录")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String", dataTypeClass = String.class,required = true),
        @ApiImplicitParam(name = "password", value = "密码", dataType = "String", dataTypeClass = String.class,required = true),
        @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", dataTypeClass = String.class,required = true)
      
    })
	@PostMapping("/login")
	public AjaxResult login(String phone,String code,String password) {
		
		if (StringUtils.isNull(phone) || StringUtils.isNull(password))
        {
            return error("参数不能为空");
        }
		
		boolean vaildateCode=userServer.validateCode(phone, code);
		if(!vaildateCode)
			return AjaxResult.error("验证码错误");
		
		UserInfo userInfo=userServer.findUserInfo(phone);
		if(ObjectUtil.isNull(userInfo)) {
			return AjaxResult.error("用户不存在");
			
		}else {
			
			AppUser appuser=	loginServer.login(userInfo, password);
			return AjaxResult.success(appuser);
		}
						
    }
	
	@ApiOperation("忘记密码")
	@ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String", dataTypeClass = String.class,required = true),
        @ApiImplicitParam(name = "password", value = "密码", dataType = "String", dataTypeClass = String.class,required = true),
        @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", dataTypeClass = String.class,required = true)
      
    })
	@PostMapping("/reset")
	public AjaxResult reset(String phone,String code,String password) {
		if (StringUtils.isNull(phone) || StringUtils.isNull(password))
        {
            return error("参数不能为空");
        }
		
		boolean vaildateCode=userServer.validateCode(phone, code);
		if(!vaildateCode)
			return AjaxResult.error("验证码错误");
		
		UserInfo userInfo=userServer.findUserInfo(phone);
		if(ObjectUtil.isNull(userInfo)) {
			return AjaxResult.error("用户不存在");
			
		}else {
		    //验证用户是否7天内已经修改过一次密码	
		    boolean vaildateTime=userServer.vaildateReset(phone);
		    if(!vaildateTime) {
		    	
		    	int result = userServer.resetPassword(phone, password);
		    	if(result==0) {
		    		return AjaxResult.error("重置密码失败");
		    	}
		    	
		    }else {
		    	
		    	return AjaxResult.error("七天内只能重置密码1次");
		    }
			
			AppUser appuser=	loginServer.login(userInfo, password);
			return AjaxResult.success(appuser);
		}
					
	}
	
	
	
	
	
	

}
